<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>tabletcanvas.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-widgets-tablet-example.html">Tablet Example</a></td><td >tabletcanvas.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">tabletcanvas.cpp 示例文件</h1>
<span class="subtitle">widgets/tablet/tabletcanvas.cpp</span>
<!-- $$$widgets/tablet/tabletcanvas.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>
  <span class="preprocessor">#include &lt;math.h&gt;</span>

  <span class="preprocessor">#include &quot;tabletcanvas.h&quot;</span>

  TabletCanvas<span class="operator">::</span>TabletCanvas()
    : <span class="type"><a href="qwidget.html">QWidget</a></span>(nullptr)
    <span class="operator">,</span> m_alphaChannelValuator(TangentialPressureValuator)
    <span class="operator">,</span> m_colorSaturationValuator(NoValuator)
    <span class="operator">,</span> m_lineWidthValuator(PressureValuator)
    <span class="operator">,</span> m_color(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>red)
    <span class="operator">,</span> m_brush(m_color)
    <span class="operator">,</span> m_pen(m_brush<span class="operator">,</span> <span class="number">1.0</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SolidLine<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RoundCap<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RoundJoin)
    <span class="operator">,</span> m_deviceDown(<span class="keyword">false</span>)
  {
      resize(<span class="number">500</span><span class="operator">,</span> <span class="number">500</span>);
      setAutoFillBackground(<span class="keyword">true</span>);
      setAttribute(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>WA_TabletTracking);
  }

  bool TabletCanvas<span class="operator">::</span>saveImage(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>file)
  {
      <span class="keyword">return</span> m_pixmap<span class="operator">.</span>save(file);
  }

  bool TabletCanvas<span class="operator">::</span>loadImage(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>file)
  {
      bool success <span class="operator">=</span> m_pixmap<span class="operator">.</span>load(file);

      <span class="keyword">if</span> (success) {
          update();
          <span class="keyword">return</span> <span class="keyword">true</span>;
      }
      <span class="keyword">return</span> <span class="keyword">false</span>;
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>clear()
  {
      m_pixmap<span class="operator">.</span>fill(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white);
      update();
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>tabletEvent(<span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">switch</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type()) {
          <span class="keyword">case</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>TabletPress:
              <span class="keyword">if</span> (<span class="operator">!</span>m_deviceDown) {
                  m_deviceDown <span class="operator">=</span> <span class="keyword">true</span>;
                  lastPoint<span class="operator">.</span>pos <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>posF();
                  lastPoint<span class="operator">.</span>pressure <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>pressure();
                  lastPoint<span class="operator">.</span>rotation <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>rotation();
              }
              <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>TabletMove:
  <span class="preprocessor">#ifndef Q_OS_IOS</span>
              <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>device() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>RotationStylus)
                  updateCursor(event);
  <span class="preprocessor">#endif</span>
              <span class="keyword">if</span> (m_deviceDown) {
                  updateBrush(event);
                  <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>m_pixmap);
                  paintPixmap(painter<span class="operator">,</span> event);
                  lastPoint<span class="operator">.</span>pos <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>posF();
                  lastPoint<span class="operator">.</span>pressure <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>pressure();
                  lastPoint<span class="operator">.</span>rotation <span class="operator">=</span> event<span class="operator">-</span><span class="operator">&gt;</span>rotation();
              }
              <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>TabletRelease:
              <span class="keyword">if</span> (m_deviceDown <span class="operator">&amp;</span><span class="operator">&amp;</span> event<span class="operator">-</span><span class="operator">&gt;</span>buttons() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoButton)
                  m_deviceDown <span class="operator">=</span> <span class="keyword">false</span>;
              update();
              <span class="keyword">break</span>;
          <span class="keyword">default</span>:
              <span class="keyword">break</span>;
      }
      event<span class="operator">-</span><span class="operator">&gt;</span>accept();
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>initPixmap()
  {
      <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> dpr <span class="operator">=</span> devicePixelRatioF();
      <span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span> newPixmap <span class="operator">=</span> <span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span>(width() <span class="operator">*</span> dpr<span class="operator">,</span> height() <span class="operator">*</span> dpr);
      newPixmap<span class="operator">.</span>setDevicePixelRatio(dpr);
      newPixmap<span class="operator">.</span>fill(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white);
      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>newPixmap);
      <span class="keyword">if</span> (<span class="operator">!</span>m_pixmap<span class="operator">.</span>isNull())
          painter<span class="operator">.</span>drawPixmap(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> m_pixmap);
      painter<span class="operator">.</span>end();
      m_pixmap <span class="operator">=</span> newPixmap;
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (m_pixmap<span class="operator">.</span>isNull())
          initPixmap();
      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="keyword">this</span>);
      <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> pixmapPortion <span class="operator">=</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(event<span class="operator">-</span><span class="operator">&gt;</span>rect()<span class="operator">.</span>topLeft() <span class="operator">*</span> devicePixelRatioF()<span class="operator">,</span>
                                  event<span class="operator">-</span><span class="operator">&gt;</span>rect()<span class="operator">.</span>size() <span class="operator">*</span> devicePixelRatioF());
      painter<span class="operator">.</span>drawPixmap(event<span class="operator">-</span><span class="operator">&gt;</span>rect()<span class="operator">.</span>topLeft()<span class="operator">,</span> m_pixmap<span class="operator">,</span> pixmapPortion);
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>paintPixmap(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">&amp;</span>painter<span class="operator">,</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">static</span> <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> maxPenRadius <span class="operator">=</span> pressureToWidth(<span class="number">1.0</span>);
      painter<span class="operator">.</span>setRenderHint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>Antialiasing);

      <span class="keyword">switch</span> (event<span class="operator">-</span><span class="operator">&gt;</span>device()) {
          <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Airbrush:
              {
                  painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoPen);
                  <span class="type"><a href="../qtgui/qradialgradient.html">QRadialGradient</a></span> grad(lastPoint<span class="operator">.</span>pos<span class="operator">,</span> m_pen<span class="operator">.</span>widthF() <span class="operator">*</span> <span class="number">10.0</span>);
                  <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color <span class="operator">=</span> m_brush<span class="operator">.</span>color();
                  color<span class="operator">.</span>setAlphaF(color<span class="operator">.</span>alphaF() <span class="operator">*</span> <span class="number">0.25</span>);
                  grad<span class="operator">.</span>setColorAt(<span class="number">0</span><span class="operator">,</span> m_brush<span class="operator">.</span>color());
                  grad<span class="operator">.</span>setColorAt(<span class="number">0.5</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>transparent);
                  painter<span class="operator">.</span>setBrush(grad);
                  <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> radius <span class="operator">=</span> grad<span class="operator">.</span>radius();
                  painter<span class="operator">.</span>drawEllipse(event<span class="operator">-</span><span class="operator">&gt;</span>posF()<span class="operator">,</span> radius<span class="operator">,</span> radius);
                  update(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(event<span class="operator">-</span><span class="operator">&gt;</span>pos() <span class="operator">-</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(radius<span class="operator">,</span> radius)<span class="operator">,</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(radius <span class="operator">*</span> <span class="number">2</span><span class="operator">,</span> radius <span class="operator">*</span> <span class="number">2</span>)));
              }
              <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>RotationStylus:
              {
                  m_brush<span class="operator">.</span>setStyle(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>SolidPattern);
                  painter<span class="operator">.</span>setPen(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>NoPen);
                  painter<span class="operator">.</span>setBrush(m_brush);
                  <span class="type"><a href="../qtgui/qpolygonf.html">QPolygonF</a></span> poly;
                  <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> halfWidth <span class="operator">=</span> pressureToWidth(lastPoint<span class="operator">.</span>pressure);
                  <span class="type"><a href="../qtcore/qpointf.html">QPointF</a></span> brushAdjust(<a href="../qtcore/qtmath.html#qSin">qSin</a>(<a href="../qtcore/qtmath.html#qDegreesToRadians-1">qDegreesToRadians</a>(<span class="operator">-</span>lastPoint<span class="operator">.</span>rotation)) <span class="operator">*</span> halfWidth<span class="operator">,</span>
                                      <a href="../qtcore/qtmath.html#qCos">qCos</a>(<a href="../qtcore/qtmath.html#qDegreesToRadians-1">qDegreesToRadians</a>(<span class="operator">-</span>lastPoint<span class="operator">.</span>rotation)) <span class="operator">*</span> halfWidth);
                  poly <span class="operator">&lt;</span><span class="operator">&lt;</span> lastPoint<span class="operator">.</span>pos <span class="operator">+</span> brushAdjust;
                  poly <span class="operator">&lt;</span><span class="operator">&lt;</span> lastPoint<span class="operator">.</span>pos <span class="operator">-</span> brushAdjust;
                  halfWidth <span class="operator">=</span> m_pen<span class="operator">.</span>widthF();
                  brushAdjust <span class="operator">=</span> <span class="type"><a href="../qtcore/qpointf.html">QPointF</a></span>(<a href="../qtcore/qtmath.html#qSin">qSin</a>(<a href="../qtcore/qtmath.html#qDegreesToRadians-1">qDegreesToRadians</a>(<span class="operator">-</span>event<span class="operator">-</span><span class="operator">&gt;</span>rotation())) <span class="operator">*</span> halfWidth<span class="operator">,</span>
                                        <a href="../qtcore/qtmath.html#qCos">qCos</a>(<a href="../qtcore/qtmath.html#qDegreesToRadians-1">qDegreesToRadians</a>(<span class="operator">-</span>event<span class="operator">-</span><span class="operator">&gt;</span>rotation())) <span class="operator">*</span> halfWidth);
                  poly <span class="operator">&lt;</span><span class="operator">&lt;</span> event<span class="operator">-</span><span class="operator">&gt;</span>posF() <span class="operator">-</span> brushAdjust;
                  poly <span class="operator">&lt;</span><span class="operator">&lt;</span> event<span class="operator">-</span><span class="operator">&gt;</span>posF() <span class="operator">+</span> brushAdjust;
                  painter<span class="operator">.</span>drawConvexPolygon(poly);
                  update(poly<span class="operator">.</span>boundingRect()<span class="operator">.</span>toRect());
              }
              <span class="keyword">break</span>;
          <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Puck:
          <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>FourDMouse:
              {
                  <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> error(tr(<span class="string">&quot;This input device is not supported by the example.&quot;</span>));
  <span class="preprocessor">#if QT_CONFIG(statustip)</span>
                  <span class="type"><a href="../qtgui/qstatustipevent.html">QStatusTipEvent</a></span> status(error);
                  <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>sendEvent(<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>status);
  <span class="preprocessor">#else</span>
                  <a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> error;
  <span class="preprocessor">#endif</span>
              }
              <span class="keyword">break</span>;
          <span class="keyword">default</span>:
              {
                  <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> error(tr(<span class="string">&quot;Unknown tablet device - treating as stylus&quot;</span>));
  <span class="preprocessor">#if QT_CONFIG(statustip)</span>
                  <span class="type"><a href="../qtgui/qstatustipevent.html">QStatusTipEvent</a></span> status(error);
                  <span class="type"><a href="qapplication.html">QApplication</a></span><span class="operator">::</span>sendEvent(<span class="keyword">this</span><span class="operator">,</span> <span class="operator">&amp;</span>status);
  <span class="preprocessor">#else</span>
                  <a href="../qtcore/qtglobal.html#qWarning">qWarning</a>() <span class="operator">&lt;</span><span class="operator">&lt;</span> error;
  <span class="preprocessor">#endif</span>
              }
              Q_FALLTHROUGH();
          <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Stylus:
              painter<span class="operator">.</span>setPen(m_pen);
              painter<span class="operator">.</span>drawLine(lastPoint<span class="operator">.</span>pos<span class="operator">,</span> event<span class="operator">-</span><span class="operator">&gt;</span>posF());
              update(<span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(lastPoint<span class="operator">.</span>pos<span class="operator">.</span>toPoint()<span class="operator">,</span> event<span class="operator">-</span><span class="operator">&gt;</span>pos())<span class="operator">.</span>normalized()
                     <span class="operator">.</span>adjusted(<span class="operator">-</span>maxPenRadius<span class="operator">,</span> <span class="operator">-</span>maxPenRadius<span class="operator">,</span> maxPenRadius<span class="operator">,</span> maxPenRadius));
              <span class="keyword">break</span>;
      }
  }

  <span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> TabletCanvas<span class="operator">::</span>pressureToWidth(<span class="type"><a href="../qtcore/qtglobal.html#qreal-typedef">qreal</a></span> pressure)
  {
      <span class="keyword">return</span> pressure <span class="operator">*</span> <span class="number">10</span> <span class="operator">+</span> <span class="number">1</span>;
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>updateBrush(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="type">int</span> hue<span class="operator">,</span> saturation<span class="operator">,</span> value<span class="operator">,</span> alpha;
      m_color<span class="operator">.</span>getHsv(<span class="operator">&amp;</span>hue<span class="operator">,</span> <span class="operator">&amp;</span>saturation<span class="operator">,</span> <span class="operator">&amp;</span>value<span class="operator">,</span> <span class="operator">&amp;</span>alpha);

      <span class="type">int</span> vValue <span class="operator">=</span> <span class="type">int</span>(((event<span class="operator">-</span><span class="operator">&gt;</span>yTilt() <span class="operator">+</span> <span class="number">60.0</span>) <span class="operator">/</span> <span class="number">120.0</span>) <span class="operator">*</span> <span class="number">255</span>);
      <span class="type">int</span> hValue <span class="operator">=</span> <span class="type">int</span>(((event<span class="operator">-</span><span class="operator">&gt;</span>xTilt() <span class="operator">+</span> <span class="number">60.0</span>) <span class="operator">/</span> <span class="number">120.0</span>) <span class="operator">*</span> <span class="number">255</span>);

      <span class="keyword">switch</span> (m_alphaChannelValuator) {
          <span class="keyword">case</span> PressureValuator:
              m_color<span class="operator">.</span>setAlphaF(event<span class="operator">-</span><span class="operator">&gt;</span>pressure());
              <span class="keyword">break</span>;
          <span class="keyword">case</span> TangentialPressureValuator:
              <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>device() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Airbrush)
                  m_color<span class="operator">.</span>setAlphaF(<a href="../qtcore/qtglobal.html#qMax">qMax</a>(<span class="number">0.01</span><span class="operator">,</span> (event<span class="operator">-</span><span class="operator">&gt;</span>tangentialPressure() <span class="operator">+</span> <span class="number">1.0</span>) <span class="operator">/</span> <span class="number">2.0</span>));
              <span class="keyword">else</span>
                  m_color<span class="operator">.</span>setAlpha(<span class="number">255</span>);
              <span class="keyword">break</span>;
          <span class="keyword">case</span> TiltValuator:
              m_color<span class="operator">.</span>setAlpha(maximum(abs(vValue <span class="operator">-</span> <span class="number">127</span>)<span class="operator">,</span> abs(hValue <span class="operator">-</span> <span class="number">127</span>)));
              <span class="keyword">break</span>;
          <span class="keyword">default</span>:
              m_color<span class="operator">.</span>setAlpha(<span class="number">255</span>);
      }

      <span class="keyword">switch</span> (m_colorSaturationValuator) {
          <span class="keyword">case</span> VTiltValuator:
              m_color<span class="operator">.</span>setHsv(hue<span class="operator">,</span> vValue<span class="operator">,</span> value<span class="operator">,</span> alpha);
              <span class="keyword">break</span>;
          <span class="keyword">case</span> HTiltValuator:
              m_color<span class="operator">.</span>setHsv(hue<span class="operator">,</span> hValue<span class="operator">,</span> value<span class="operator">,</span> alpha);
              <span class="keyword">break</span>;
          <span class="keyword">case</span> PressureValuator:
              m_color<span class="operator">.</span>setHsv(hue<span class="operator">,</span> <span class="type">int</span>(event<span class="operator">-</span><span class="operator">&gt;</span>pressure() <span class="operator">*</span> <span class="number">255.0</span>)<span class="operator">,</span> value<span class="operator">,</span> alpha);
              <span class="keyword">break</span>;
          <span class="keyword">default</span>:
              ;
      }

      <span class="keyword">switch</span> (m_lineWidthValuator) {
          <span class="keyword">case</span> PressureValuator:
              m_pen<span class="operator">.</span>setWidthF(pressureToWidth(event<span class="operator">-</span><span class="operator">&gt;</span>pressure()));
              <span class="keyword">break</span>;
          <span class="keyword">case</span> TiltValuator:
              m_pen<span class="operator">.</span>setWidthF(maximum(abs(vValue <span class="operator">-</span> <span class="number">127</span>)<span class="operator">,</span> abs(hValue <span class="operator">-</span> <span class="number">127</span>)) <span class="operator">/</span> <span class="number">12</span>);
              <span class="keyword">break</span>;
          <span class="keyword">default</span>:
              m_pen<span class="operator">.</span>setWidthF(<span class="number">1</span>);
      }

      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>pointerType() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Eraser) {
          m_brush<span class="operator">.</span>setColor(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white);
          m_pen<span class="operator">.</span>setColor(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>white);
          m_pen<span class="operator">.</span>setWidthF(event<span class="operator">-</span><span class="operator">&gt;</span>pressure() <span class="operator">*</span> <span class="number">10</span> <span class="operator">+</span> <span class="number">1</span>);
      } <span class="keyword">else</span> {
          m_brush<span class="operator">.</span>setColor(m_color);
          m_pen<span class="operator">.</span>setColor(m_color);
      }
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>updateCursor(<span class="keyword">const</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="type"><a href="../qtgui/qcursor.html">QCursor</a></span> cursor;
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>type() <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qevent.html">QEvent</a></span><span class="operator">::</span>TabletLeaveProximity) {
          <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>pointerType() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Eraser) {
              cursor <span class="operator">=</span> <span class="type"><a href="../qtgui/qcursor.html">QCursor</a></span>(<span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span>(<span class="string">&quot;:/images/cursor-eraser.png&quot;</span>)<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">28</span>);
          } <span class="keyword">else</span> {
              <span class="keyword">switch</span> (event<span class="operator">-</span><span class="operator">&gt;</span>device()) {
              <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Stylus:
                  cursor <span class="operator">=</span> <span class="type"><a href="../qtgui/qcursor.html">QCursor</a></span>(<span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span>(<span class="string">&quot;:/images/cursor-pencil.png&quot;</span>)<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
                  <span class="keyword">break</span>;
              <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>Airbrush:
                  cursor <span class="operator">=</span> <span class="type"><a href="../qtgui/qcursor.html">QCursor</a></span>(<span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span>(<span class="string">&quot;:/images/cursor-airbrush.png&quot;</span>)<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">4</span>);
                  <span class="keyword">break</span>;
              <span class="keyword">case</span> <span class="type"><a href="../qtgui/qtabletevent.html">QTabletEvent</a></span><span class="operator">::</span>RotationStylus: {
                  <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> origImg(QLatin1String(<span class="string">&quot;:/images/cursor-felt-marker.png&quot;</span>));
                  <span class="type"><a href="../qtgui/qimage.html">QImage</a></span> img(<span class="number">32</span><span class="operator">,</span> <span class="number">32</span><span class="operator">,</span> <span class="type"><a href="../qtgui/qimage.html">QImage</a></span><span class="operator">::</span>Format_ARGB32);
                  <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> solid <span class="operator">=</span> m_color;
                  solid<span class="operator">.</span>setAlpha(<span class="number">255</span>);
                  img<span class="operator">.</span>fill(solid);
                  <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>img);
                  <span class="type"><a href="../qtgui/qtransform.html">QTransform</a></span> transform <span class="operator">=</span> painter<span class="operator">.</span>transform();
                  transform<span class="operator">.</span>translate(<span class="number">16</span><span class="operator">,</span> <span class="number">16</span>);
                  transform<span class="operator">.</span>rotate(event<span class="operator">-</span><span class="operator">&gt;</span>rotation());
                  painter<span class="operator">.</span>setTransform(transform);
                  painter<span class="operator">.</span>setCompositionMode(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>CompositionMode_DestinationIn);
                  painter<span class="operator">.</span>drawImage(<span class="operator">-</span><span class="number">24</span><span class="operator">,</span> <span class="operator">-</span><span class="number">24</span><span class="operator">,</span> origImg);
                  painter<span class="operator">.</span>setCompositionMode(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span><span class="operator">::</span>CompositionMode_HardLight);
                  painter<span class="operator">.</span>drawImage(<span class="operator">-</span><span class="number">24</span><span class="operator">,</span> <span class="operator">-</span><span class="number">24</span><span class="operator">,</span> origImg);
                  painter<span class="operator">.</span>end();
                  cursor <span class="operator">=</span> <span class="type"><a href="../qtgui/qcursor.html">QCursor</a></span>(<span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span><span class="operator">::</span>fromImage(img)<span class="operator">,</span> <span class="number">16</span><span class="operator">,</span> <span class="number">16</span>);
              } <span class="keyword">break</span>;
              <span class="keyword">default</span>:
                  <span class="keyword">break</span>;
              }
          }
      }
      setCursor(cursor);
  }

  <span class="type">void</span> TabletCanvas<span class="operator">::</span>resizeEvent(<span class="type"><a href="../qtgui/qresizeevent.html">QResizeEvent</a></span> <span class="operator">*</span>)
  {
      initPixmap();
  }

</pre>
</div>
<!-- @@@widgets/tablet/tabletcanvas.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
